<?php 
set_time_limit(0);
include_once 'function.php';

error_reporting(E_ALL);
// 2. remove old datas from table.
db_query("delete from routers");

$offset = 0; 

while (true) {

// 1. download xls file
//$url = "https://onionoo.torproject.org/details?limit=4";
$url = "https://onionoo.torproject.org/details?offset=".$offset."&limit=1000";
echo $url."<br/>";
$filecontent = file_get_contents($url);
//$filecontent = utf8_encode($filecontent); 
$results = json_decode($filecontent); 
$rows = $results->relays;


// 3. import new datas to table 
$first = true;
$insertCnt = 0;

$sql = "insert into routers (ipaddr, hostname, routername, uptime, platform, exitrelay, country, `lat`, `long`) values ";
$sqlValList = array();
foreach ($rows as $row) {
	$upTime = (time() - strtotime($row->last_restarted))/(60*60);
	$exitdelay = $row->running == "true" ? 0 : 1;
	$sqlValList[] = "('{$row->or_addresses[0]}', '{$row->host_name}', '{$row->nickname}', '{$upTime}','{$row->platform}', '{$exitdelay}', '{$row->country_name}', '{$row->latitude}', '{$row->longitude}')";
	$insertCnt++;
	//if ($insertCnt > 10) break;
}

if ($insertCnt == 0) exit();
$sql .= implode(",", $sqlValList);



db_query($sql);


echo "Inserted ".$insertCnt. " rows<br/>";

$offset += 1000;

}